package dm.bsi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.SQLException;

import org.dom4j.DocumentException;

import indi.zjh.dp.db.Session;
import indi.zjh.dp.error.UnsuportedFieldException;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Relation;
import indi.zjh.dp.orm.Result;
import indi.zjh.dp.type.String128;
import indi.zjh.dp.type.String256;

public class Image extends LogNode {
	protected String256 name = new String256();
	protected String128 oid = new String128();
	
	protected Relation container = new Relation(Container.class);
	protected Relation deployments = new Relation(Deployment.class);
	protected Relation nodes = new Relation(Node.class);
	
	/**
	 * 根据镜像名称获取镜像
	 * @param session
	 * @param name
	 * @return
	 * @throws SQLException
	 * @throws DocumentException
	 * @throws UnsuportedFieldException
	 * @throws Exception
	 */
	public static Image getImageByName(Session session, String name) throws SQLException, DocumentException, UnsuportedFieldException, Exception
	{
		// 检查镜像名称是否存在
		Query<Image> imageQuery = new Query<Image>(session);
		Result<Image> imageResult = imageQuery.find(Image.class, "`name` = '"+name+"'").result();
		Image image = null;
		if(imageResult.count() > 0)
		{
			image = imageResult.iterator().next();
			return image;
		}
		
		return null;
	}
	
	public static String toDictString(Method m)
	{
		if(m.getName().equals("getImageByName"))
		{
			return "根据镜像名称查找镜像";
		}
		return LogNode.toDictString(m);
	}
	
	public static String toDictString(Field f)
	{
		if(f.getName().equals("name"))
		{
			return "镜像名称";
		}
		else if(f.getName().equals("container"))
		{
			return "关联的容器对象";
		}
		else if(f.getName().equals("oid"))
		{
			return "docker分配给镜像的唯一标识码";
		}
		else if(f.getName().equals("container"))
		{
			return "关联的容器";
		}
		else if(f.getName().equals("deployments"))
		{
			return "关联的部署";
		}
		else if(f.getName().equals("nodes"))
		{
			return "关联的节点";
		}
		return LogNode.toDictString(f);
		
	}

	public String getName() {
		return name.string;
	}

	public void setName(String name) {
		this.name.string = name;
	}

	public String getOid() {
		return oid.string;
	}
	public void setOid(String oid) {
		this.oid.string = oid;
	}
	
	public Relation getContainer() {
		return container;
	}

	public Relation getDeployments() {
		return deployments;
	}

	public Relation getNodes() {
		return nodes;
	}

	@Override
	public String toString() {
		return "Image [name=" + name + ", oid=" + oid + ", container=" + container + ", deployments=" + deployments
				+ ", nodes=" + nodes + ", _id=" + _id + ", _v=" + _v + "]";
	}
	
	
}
